//
//  GoodAttributeListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/22.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "GoodAttributeListVC.h"
#import "GoodAttributeListModel.h"
#import "GoodAttributeListCell.h"
#import "GoodAttributeEditVC.h"

@interface GoodAttributeListVC ()<UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <GoodAttributeListModel *>*array;

@end

@implementation GoodAttributeListVC

-(NSMutableArray<GoodAttributeListModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
//            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page++;
//            [strongSelf requestForList];
//        }];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"属性模板";
//    page = 1;
    [self.view addSubview:self.tableView];
    [self requestForList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshList) name:ReceiptDeliveryRefreshGoodAttributeList object:nil];
}

- (void)refreshList {
//    page = 1;
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryGoodManagerGoodAttributeList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
//            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [GoodAttributeListModel mj_objectArrayWithKeyValuesArray:responseObject];
//            }else {
//                NSArray *array = [GoodAttributeListModel mj_objectArrayWithKeyValuesArray:responseObject];
//                [self.array addObjectsFromArray:array];
//            }
//            BOOL has_more = [responseObject[@"has_more"] boolValue];
//            if (has_more) {
//                self.tableView.mj_footer.hidden = NO;
//            }else {
//                self.tableView.mj_footer.hidden = YES;
//            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
//            [self.tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodAttributeListCell *cell = [GoodAttributeListCell GoodAttributeListCellWithTableView:tableView];
    GoodAttributeListModel *model = self.array[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.editBlock = ^{
        GoodAttributeEditVC *vc = [GoodAttributeEditVC new];
        vc.Id = model.Id;
        vc.n_id = model.n_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GoodAttributeListCell *cell = [GoodAttributeListCell GoodAttributeListCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
